﻿///////////////////////////////////////////////////////////////////////////////////////////////////
//------------------------------版权声明----------------------------
//
// 此文件为 SuperMap iObjects .NET 的示范代码
// 版权所有：北京超图软件股份有限公司
//------------------------------------------------------------------
//
//-----------------------SuperMap iObjects .NET 示范程序说明--------------------------
//
//1、范例简介：演示如何实现鹰眼，放大镜功能；
//2、示例数据：安装目录\SampleData\World\World.smwu；
//3、关键类型/成员: 
//      Workspace.Open 方法
//      MapControl.Map 属性
//      Map.Layers 属性 
//      Point2Ds.Add 方法
//      TrackingLayer.Add 方法
//      GeoLine.Style 属性
//      
//4、使用步骤：
//   (1)进行地图操作。
//   (2)选择是否使用放大镜。
//   (3)设置放大镜倍数。
//   (4)左键点击鹰眼图，鹰眼图矩形框移动到鼠标位置，在主视图和放大镜视图中显示内容。
//   (5)在鹰眼视图的矩形框内按住鼠标左键拖动矩形框，在主视图和放大镜视图中显示相应内容。
//---------------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using SuperMap.Data;
using SuperMap.UI;
using SuperMap.Mapping;

namespace SuperMap.SampleCode.Mapping
{
    public class SampleRun
    {
        private Workspace m_workspace;
        private MapControl m_mapControlMain;
        private MapControl m_mapControlEagleEye;
        private MapControl m_mapControlMagnifier;

        private Rectangle2D m_engleRectangle;
        private Point m_pointBefore;
        private Boolean m_isMoveEngleRect;

        private Int32 m_selectScaleIndex;
        public Int32 SelectScaleIndex
        {
            set { m_selectScaleIndex = value; }
        }

        private Boolean m_isUseMagnifierChecked;
        public Boolean IsUseMagnifierChecked
        {
            set { m_isUseMagnifierChecked = value; }
        }
        /// <summary>
        /// 根据workspace和map构造 SampleRun对象
        /// </summary>
        public SampleRun(Workspace workspace, MapControl mapControlMain, MapControl mapControlEagleEye, MapControl mapControlMagnifier)
        {
            m_workspace = workspace;
            m_mapControlMain = mapControlMain;
            m_mapControlEagleEye = mapControlEagleEye;
            m_mapControlMagnifier = mapControlMagnifier;

            m_mapControlMain.Map.Workspace = workspace;
            m_mapControlEagleEye.Map.Workspace = workspace;
            m_mapControlMagnifier.Map.Workspace = workspace;
            //定义m_mapControl、m_mapControlEagleEye、m_mapControlMagnifier鼠标事件
            m_mapControlEagleEye.MouseMove += new MouseEventHandler(EagleEyeMapMouseMoveHandler);
            m_mapControlEagleEye.MouseDown += new MouseEventHandler(EagleEyeMapMouseDownHandler);
            m_mapControlEagleEye.MouseUp += new MouseEventHandler(EagleEyeMapMouseUpHandler);
            m_mapControlEagleEye.ActionCursorChanging += new ActionCursorChangingEventHandler(EagleEyeMapCursorChangedHandler);

            m_mapControlMain.Map.Drawn += new MapDrawnEventHandler(MainMapDrawnHandler);

            Initialize();
        }
        /// <summary>
        /// 打开需要的工作空间文件及地图
        /// </summary>
        private void Initialize()
        {
            try
            {
                //打开工作空间及地图
                WorkspaceConnectionInfo workspaceConInfo = new WorkspaceConnectionInfo(@"..\..\SampleData\World\World.smwu");
                if (m_workspace.Open(workspaceConInfo))
                {
                    String mapName = m_workspace.Maps[1];

                    // 调整mapControl的状态
                    m_mapControlMain.Action = SuperMap.UI.Action.Pan;
                    m_mapControlMain.Map.Center = new Point2D(117.28, 39.11);
                    m_mapControlMain.Map.Scale = 0.00455;
                    //  在主地图视图中打开地图
                    m_mapControlMain.Map.Open(mapName);
                    m_mapControlMain.IsWaitCursorEnabled = false;
                    m_mapControlMain.Map.Refresh();
                    //  在鹰眼视图中打开地图
                    m_mapControlEagleEye.Map.Open(mapName);

                    foreach (Layer layer in m_mapControlEagleEye.Map.Layers)
                    {
                        layer.IsSelectable = false;
                    }

                    m_mapControlEagleEye.Map.ViewEntire();
                    m_mapControlEagleEye.Map.Refresh();
                    m_mapControlEagleEye.MarginPanEnabled = false;
                    m_mapControlEagleEye.IsWaitCursorEnabled = false;
                    m_mapControlEagleEye.InteractionMode = InteractionMode.CustomAll;
                    m_mapControlEagleEye.Cursor = Cursors.Arrow;
                    //  在放大镜视图中打开地图
                    m_mapControlMagnifier.Map.Open(mapName);

                    foreach (Layer layer in m_mapControlMagnifier.Map.Layers)
                    {
                        layer.IsSelectable = false;
                    }

                    m_mapControlMagnifier.Map.Refresh();
                    m_mapControlMagnifier.MarginPanEnabled = false;
                    m_mapControlMagnifier.IsWaitCursorEnabled = false;
                    m_mapControlMagnifier.Action = SuperMap.UI.Action.Null;
                    m_mapControlMagnifier.InteractionMode = InteractionMode.CustomAll;
                }
                else
                {
                    MessageBox.Show("打开工作空间失败");
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 定义地图绘制结束事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainMapDrawnHandler(object sender, MapDrawnEventArgs e)
        {
            if (!m_isMoveEngleRect)
            {
                DisplayRect(m_mapControlMain.Map.ViewBounds);
            }

            ZoomForView(m_selectScaleIndex);
        }

        /// <summary>
        /// 定义鹰眼窗口鼠标按下事件，并将鼠标按下时的坐标保存到pointBefore
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EagleEyeMapMouseDownHandler(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                m_pointBefore = e.Location;
            }
        }
        /// <summary>
        /// 定义鹰眼窗口中鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EagleEyeMapMouseMoveHandler(object sender, MouseEventArgs e)
        {
            m_mapControlEagleEye.Cursor = Cursors.Arrow;

            try
            {
                MapControl eagleEyeMapControl = sender as MapControl;
                Map eagleEyeMap = eagleEyeMapControl.Map;
                //当鼠标是在rect框内时，改变鼠标形态，对改变鼠标形态进行控制
                if (m_engleRectangle.Contains(eagleEyeMap.PixelToMap(e.Location)))
                {
                    m_mapControlEagleEye.Cursor = Cursors.Cross;

                    if (e.Button == MouseButtons.Left)
                    {
                        m_isMoveEngleRect = true;
                    }
                }

                if (m_isMoveEngleRect)
                {
                    //将pointBefore点和当前鼠标点的像素坐标转换为地图坐标
                    //计算两点的坐标差对rect进行重新绘制
                    Point2D point2DBeforeMove = m_mapControlEagleEye.Map.PixelToMap(m_pointBefore);
                    Point2D point2DAfterMove = m_mapControlEagleEye.Map.PixelToMap(e.Location);

                    Double dx = point2DAfterMove.X - point2DBeforeMove.X;
                    Double dy = point2DAfterMove.Y - point2DBeforeMove.Y;

                    m_engleRectangle = new Rectangle2D(OffsetPoint(new Point2D(m_engleRectangle.Left, m_engleRectangle.Bottom), dx, dy), OffsetPoint(new Point2D(m_engleRectangle.Right, m_engleRectangle.Top), dx, dy));

                    DisplayRect(m_engleRectangle);
                    m_mapControlMain.Map.ViewBounds = m_engleRectangle;
                    m_mapControlMain.Map.Refresh();
                    //将鼠标的当前点坐标存入pointBefore，以进行循环计算
                    m_pointBefore = e.Location;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 定义鼠标释放事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EagleEyeMapMouseUpHandler(object sender, MouseEventArgs e)
        {
            try
            {
                //左键释放，定义isMoveEngleRect为false
                if (e.Button == MouseButtons.Left)
                {
                    if (!m_isMoveEngleRect)
                    {
                        Point2D pntCenter = m_mapControlEagleEye.Map.PixelToMap(e.Location);
                        m_mapControlMain.Map.Center = pntCenter;
                        m_mapControlMain.Map.Refresh();
                    }

                    m_isMoveEngleRect = false;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 定义更新光标事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EagleEyeMapCursorChangedHandler(object sender, ActionCursorChangingEventArgs e)
        {
            e.FollowingCursor = Cursors.Cross;
        }

        /// <summary>
        /// 放大镜视图按比例放大显示
        /// </summary>
        public void ZoomForView(Int32 index)
        {
            try
            {
                Point2D pntCenter = m_mapControlMain.Map.Center;
                m_mapControlMagnifier.Map.Center = pntCenter;

                //  选择地图放大倍数，放大镜窗口放大显示
                if (m_isUseMagnifierChecked)
                {
                    double dScale = m_mapControlMain.Map.Scale;

                    switch (index)
                    {
                        case 0:
                            {
                                m_mapControlMagnifier.Map.Scale = dScale;
                            }
                            break;
                        case 1:
                            {
                                m_mapControlMagnifier.Map.Scale = dScale * 2;
                            }
                            break;
                        case 3:
                            {
                                m_mapControlMagnifier.Map.Scale = dScale * 4;
                            }
                            break;
                        case 4:
                            {
                                m_mapControlMagnifier.Map.Scale = dScale * 5;
                            }
                            break;
                        case 5:
                            {
                                m_mapControlMagnifier.Map.Scale = dScale * 6;
                            }
                            break;
                        default:
                            {
                                m_mapControlMagnifier.Map.Scale = dScale * 3;
                            }
                            break;
                    }

                    m_mapControlMagnifier.Map.Refresh();
                }
                else
                {
                    m_mapControlMagnifier.Map.ViewEntire();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 按照指定风格显示矩形框
        /// </summary>
        /// <param name="rectDisplay"></param>
        private void DisplayRect(Rectangle2D rectangleDisplay)
        {
            try
            {
                m_engleRectangle = rectangleDisplay;
                m_mapControlEagleEye.Cursor = Cursors.Cross;

                Double rectangleWidth = rectangleDisplay.Width;
                Double rectangleHeight = rectangleDisplay.Height;
                Double pntLeftTopX = rectangleDisplay.Left;
                Double pntLeftTopY = rectangleDisplay.Top;
                //设置图框四点坐标
                Point2Ds points = new Point2Ds();

                Point2D pntLeftTop = new Point2D(pntLeftTopX, pntLeftTopY);
                points.Add(pntLeftTop);
                Point2D pntLeftBottom = new Point2D(pntLeftTopX, pntLeftTopY - rectangleHeight);
                points.Add(pntLeftBottom);
                Point2D pntRightBottom = new Point2D(pntLeftTopX + rectangleWidth, pntLeftTopY - rectangleHeight);
                points.Add(pntRightBottom);
                Point2D pntRightTop = new Point2D(pntLeftTopX + rectangleWidth, pntLeftTopY);
                points.Add(pntRightTop);
                points.Add(pntLeftTop);
                //将点连成线，并设置样式

                GeoLine rectangleBoundary = new GeoLine();
                rectangleBoundary.AddPart(points);

                GeoStyle rectangleStyle = new GeoStyle();
                rectangleStyle.LineColor = Color.FromArgb(255, 0, 0);
                rectangleStyle.LineWidth = 0.5;

                rectangleBoundary.Style = rectangleStyle;
                //添加到跟踪层
                m_mapControlEagleEye.Map.TrackingLayer.Clear();
                m_mapControlEagleEye.Map.TrackingLayer.Add((Geometry)rectangleBoundary, "");
                m_mapControlEagleEye.Map.Refresh();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 构造OffsetPoint点对象
        /// </summary>
        /// <param name="point"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        private Point2D OffsetPoint(Point2D point, Double dx, Double dy)
        {
            Point2D temp = point;
            temp.Offset(dx, dy);

            return temp;
        }

    }
}


